package com.github.lee.unsplash.global

import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.os.Build
import android.os.Process
import androidx.startup.AppInitializer
import com.github.lee.core.common.LogUtil
import com.github.lee.mvvm.core.startup.X5WebViewInitializer
import com.github.lee.unsplash.BuildConfig
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class UnsplashApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        LogUtil.init("Unsplash", BuildConfig.DEBUG)

        val progressName = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            getProcessName()
        } else {
            val myPid = Process.myPid()
            val am = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            am.runningAppProcesses?.forEach {
                if (it.pid == myPid) {
                    it.processName
                }
            }
            ""
        }


        if ("$packageName:web" == progressName || "$packageName:auth" == progressName) { //Web进程初始化X5
            AppInitializer.getInstance(this).initializeComponent(X5WebViewInitializer::class.java)
        }
    }
}